package com.ssm.web;

import com.ssm.Service.CategoryService;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "findAllServlet",urlPatterns = "/findAll")
public class findAllServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("ajax请求已送达");
        CategoryService categoryService=new CategoryService();

        List<Object> all = categoryService.findAll();
        response.setContentType("text/html;charset=utf-8");
//        for(Object object:all){
//            Object[] o= (Object[]) object;
//            for(Object ob:o){
//                System.out.println(ob.toString());
//            }
//        }
        //将数据转换成数组
        JSONArray data = JSONArray.fromObject(all);

        //将数据传向前端控制台
        PrintWriter writer=response.getWriter();
        writer.print(data);
        writer.close();

    }
}
